package com.ys.controller.api.outer;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ys.service.sys.UploadService;
import com.ys.vo.R;

@CrossOrigin
@Controller
@RequestMapping("/api/outer/upload")
public class ApiOuterUploadController {
	
    @Autowired
    private UploadService uploadService;

	@RequestMapping("/uploadfileForEditor")
	@ResponseBody
	public void uploadfileForEditor(Model model, HttpServletRequest request, HttpServletResponse response,
			@RequestParam("fileStream") MultipartFile file) throws FileUploadException, IOException {
		
		uploadService.uploadfileForEditor(request, response, file, null);
	}

	@PostMapping("/uploadfileForLayer")
	@ResponseBody
	public void uploadfileForLayer(Model model, HttpServletRequest request, HttpServletResponse response,
			@RequestParam("file") MultipartFile file, String moduleFile) throws FileUploadException, IOException {

		uploadService.uploadfileForLayer(response, file, moduleFile);
	}

	@RequestMapping("/uploadBase64")
	@ResponseBody
	public R uploadBase64(Model model, HttpServletRequest request, HttpServletResponse response,
			@RequestBody String body) throws IOException {
		return uploadService.uploadBase64(body);
	}

	/**
	 * 通过文件网址URL上传
	 * @throws IOException 
	 */
	@PostMapping("/uploadByUrl")
	@ResponseBody
	public R uploadByUrl(HttpServletRequest request,
			String urlOld, String moduleFile) throws IOException {
		return uploadService.uploadByUrl(urlOld, moduleFile);
	}
    
    /**
     * 上传文件（给1.0使用）
     * @param uploadfile
     * @param moduleFile 文件模块
     */
	@PostMapping("/uploadForERP_1_0")
	@ResponseBody
	public R uploadForERP_1_0(@RequestParam("file") MultipartFile uploadfile, String moduleFile) throws Exception{
		
		
        return uploadService.uploadfileFor1_0(uploadfile, moduleFile);
    }
}
